package hot100;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author aodre , QQ : 480029069
 * @date 2023/8/28 17:24
 */
public class lc49字母疑惑词分组 {




    /*
     https://leetcode.cn/problems/group-anagrams/solutions/520515/kan-wo-yi-ju-hua-ac-zi-mu-yi-wei-ci-fen-yrnis/?envType=study-plan-v2&envId=top-100-liked
     stream 真实牛逼
     */
    public List<List<String>> solve(String str[]){
        Map<String,List<String>> cache = new HashMap<>();
        List<List<String>> ans = new ArrayList<>();
        for (String s : str){
            int map[] = new int[26];
            for(char c : s.toCharArray()){
                map[c - 'a']++;
            }
            StringBuilder sb = new StringBuilder();
            for(int i = 0;i < map.length;i++){ // [b,a,a,a,b,c] 编码成  a3b2c1
                if(map[i] != 0){
                    sb.append('a' + i);
                    sb.append(map[i]);
                }
            }
            String s1 = sb.toString();
            List<String> orDefault = cache.getOrDefault(s1, new ArrayList<>());
            orDefault.add(s);
            cache.put(s1,orDefault);
        }
        for (List<String> value : cache.values()) {
            ans.add(value);
        }

        return ans;
    }


    /*
     stream 流的一个写法
     */

    public List<List<String>> solve2(String str[]){

        Arrays.stream(str).collect(Collectors.toList());
//        return new ArrayList<>(Arrays.stream(str).collect(Collectors.groupingBy(str)));
        return null;

    }






}
